// pages/palyGame/palyGame.js
const app = getApp()
const api = require('../../api.js');
const post = api.post;
const getCurrentPageUrlWithArgs = api.getCurrentPageUrlWithArgs;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        accredit: false,
        game_list: "",
        gold: '',
        kgame: '',
        uid: '',
        allowplay: '0'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        // console.log(options.t_uid);
        wx.setStorageSync('ifList', 1);
        if (!!options.t_uid) {
            that.setData({
                t_uid: options.t_uid
            })
        }
        var kgame = wx.getStorageSync('kgame');
        if (!!kgame) {
            that.setData({
                accredit: true,
                kgame: kgame
                    // accredit: app.globalData.accredit
            });
            that.listAll();
            // if (that.data.accredit) {
            //     that.listAll()
            // }
        }

    },
    listAll() {
        let that = this;
        let aaa = wx.getStorageSync('allowplay');
        let ifwj = wx.getStorageSync('ifwj');
        that.setData({
            jbxs: aaa
        })
        let p = {
            m: 'gamelist',
            kgame: that.data.kgame,
            allowplay: aaa
        }
        post(p).then(res => {
            // console.log(res);
            if (res.r == 0) {
                if (ifwj != 1) {
                    if (aaa == 0) {
                        wx.redirectTo({
                            url: '../index/index'
                        })
                    };
                } else {
                    that.setData({
                        ifwj: ifwj
                    })
                }
                that.setData({
                    game_list: res.result,
                    gold: res.info.gold,
                    uid: res.info.uid
                });
                wx.setStorageSync('uid', res.info.uid);
                var ifgold = wx.getStorageSync('allowplay');
                var gold = wx.getStorageSync('gold');
                if (ifgold == 1) {
                    if (res.info.gold > gold) {
                        gold = res.info.gold - gold
                        wx.showModal({
                            title: '',
                            content: '收获' + gold + '金币',
                            showCancel: false,
                        });
                        wx.setStorageSync('gold', res.info.gold);
                    } else {
                        wx.setStorageSync('gold', res.info.gold);
                    }
                }

            } else {
                wx.setStorageSync('weapp_openid');
                wx.setStorageSync('kgame');
                wx.setStorageSync('userInfo');
                wx.setStorageSync('uid');
                that.setData({
                    accredit: false,
                    kgame: ''
                });
            }

        })
    },
    PalyGame_J(e) {
        let that = this;
        let tishi = '试玩一次需要消耗' + e.target.dataset.usedgold + '金币，是否消耗？'
        wx.showModal({
            content: tishi,
            success(res) {
                if (res.confirm) {
                    let p = {
                        m: 'usedgold',
                        usedgold: e.target.dataset.usedgold,
                        uid: that.data.uid,
                        // allowplay: jbxs
                    }
                    post(p).then(res => {
                        if (res.r == 0) {
                            // console.log(res);
                            wx.navigateTo({
                                url: './palyJump/palyJump?path=' + e.target.dataset.path,
                            });
                        }
                    })
                }
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        var ifList = wx.getStorageSync('ifList');
        if (ifList != 1) {
            that.setData({
                game_list: '',
                gold: ''
            })
            if (that.data.accredit) {
                that.listAll();
            }
        } else {
            wx.setStorageSync('ifList', 2);
        }

    },
    navPay() {
        let navUrl = '../../pages/Pay/Pay';
        wx.navigateTo({
            url: navUrl,
        })
    },
    bindgetuserinfo(e) {
        let that = this;
        var kgame = wx.getStorageSync('kgame');
        var userInfo = wx.getStorageSync('userInfo');
        var weapp_openid = wx.getStorageSync('weapp_openid');
        if (kgame && userInfo && weapp_openid && weapp_openid == userInfo.openId) {
            that.setData({
                accredit: true,
                kgame: kgame
            });
            that.listAll()
        } else {
            // 获取用户信息
            wx.login({
                success(res) {
                    var code = res.code;
                    wx.getUserInfo({
                        success(res2) {
                            var encryptedData = res2.encryptedData;
                            var iv = res2.iv;
                            var p = {
                                m: 'regdate_member',
                                code: code,
                                encryptedData: encryptedData,
                                iv: iv
                            };
                            if (!!that.data.t_uid) {
                                p.t_uid = that.data.t_uid
                            }
                            // console.log(p);
                            post(p).then(res3 => {
                                // if (0 == res3.r) {
                                // console.log(res3);
                                var info = res3.info;
                                var openId = info.openId;
                                wx.setStorageSync('weapp_openid', openId);
                                wx.setStorageSync('kgame', info.kgame);
                                wx.setStorageSync('userInfo', info);
                                that.setData({
                                    allowplay: info.allowplay
                                });
                                wx.showToast({
                                    title: '授权成功',
                                    icon: 'none',
                                });
                                // console.log(info.allowplay);
                                wx.setStorageSync('ap', info.allowplay);
                                if (info.allowplay == 0) {
                                    let aaa = wx.getStorageSync('allowplay');
                                    if (aaa == 0) {
                                        wx.redirectTo({
                                            url: '../index/index'
                                        })
                                    } else {
                                        that.setData({
                                            accredit: true,
                                            kgame: info.kgame
                                        });
                                        that.listAll()
                                    }
                                } else {
                                    that.setData({
                                        accredit: true,
                                        kgame: info.kgame
                                    });
                                    that.listAll()
                                }
                                // }
                            })
                        },
                        fail(data) {}
                    })
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        let that = this;
        that.setData({
            game_list: ''
        });
        wx.showToast({
            title: "loading....",
            icon: 'loading',
            success(res) {
                setTimeout(() => {
                    that.listAll()
                }, 1500)
            }
        })

        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // },
    submitFormId(e) {
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let that = this;
        let p = {
            m: 'colform',
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        var uid = wx.getStorageSync('uid')
        if (!!uid) {
            p.uid = uid
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        // console.log(p);
        post(p).then(res => {})
    }
})